package com.juyu.miliao.api.controller;

import java.util.List;

import javax.annotation.Resource;

import com.juyu.miliao.api.domain.BillDetailDeposit;
import com.juyu.miliao.api.domain.vo.BillDetailBeansVo;
import com.juyu.miliao.api.domain.vo.BillDetailDepositVo;
import com.juyu.miliao.api.service.BillDetailIntegralService;
import com.juyu.miliao.common.domain.vo.BillDetailIntegralVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.service.BillDetailService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 查询用户消费详细记录
 */
@RestController
@RequestMapping("/billDetail")
public class BillDetailController {
	@Resource
	BillDetailService billDetailService;

	@Autowired
	private BillDetailIntegralService billDetailIntegralService;

	/**
	 * 查询用户消费详细记录
	 * 
	 * @param userId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/{userId:^[1-9][0-9]*$}/{page}-{pageSize}")
	@JsonView(JacksonView.BillDetail.class)
	public List<BillDetail> getBillDetailByUserId(Authentication token, @PathVariable("userId") Integer userId,
			@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return billDetailService.getBillDetailByUserId(userId, page, pageSize);
	}
//	/**
//	 * 获取幸运红包消费记录
//	 * 
//	 * @param id
//	 * @return
//	 */
//	@GetMapping("/redPacket/{id:^[1-9][0-9]*$}/{page}-{pageSize}")
//	@JsonView(JacksonView.RedPacket.class)
//	public List<BillDetail> getRedPacket(Authentication token, @PathVariable("id") Integer id,
//			@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
//		Subject subject = (Subject) token.getPrincipal();
//		if (subject.getId() != id)
//			throw new IllegalParamException("用户id错误");
//		if (page <= 0) {
//			page = 1;
//		}
//		if (pageSize <= 0) {
//			pageSize = 20;
//		}
//		return billDetailService.getRedPacketById(id, page, pageSize);
//	}

	/**
	 * 查询用户寻豆消费详细记录
	 *
	 * @param userId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/beans/{userId:^[1-9][0-9]*$}/{page}-{pageSize}")

	public List<BillDetailBeansVo> getBillDetailBeanByUserId(Authentication token, @PathVariable("userId") long userId,
															 @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return billDetailService.getBillDetailBeansByUserId(userId, page, pageSize);
	}
	/**
	 * 查询用户金币消费详细记录
	 *
	 * @param userId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/deposit/{userId:^[1-9][0-9]*$}/{coinsType}/{page}-{pageSize}")
	public List<BillDetailDepositVo> getBillDetailDepositByUserId(Authentication token, @PathVariable("userId") long userId,
																  @PathVariable("coinsType") Integer coinsType, @PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return billDetailService.getBillDetailDepositByUserId(userId,coinsType, page, pageSize);
	}


	@GetMapping("/integral/{userId:^[1-9][0-9]*$}/{page}-{pageSize}")
	public List<BillDetailIntegralVO> getBillDetailIntegralByUserId(@PathVariable("userId") long userId,
																	@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		if (pageSize <= 0) {
			pageSize = 20;
		}
		if (page <= 0) {
			page = 1;
		}
		return billDetailIntegralService.getBillDetailIntegralByUserId(userId, page, pageSize);
	}
}
